package Popup_Menu;
import javax.swing.*;
import java.awt.event.*;

public class MyFrame extends JFrame implements MouseListener{
    JPopupMenu pm;
    public MyFrame()
    {
        this.setSize(350,200);
        this.setTitle("Popup Menu Demo.");
        this.setLayout(null); // 将JFrame的布局设置为空，通过坐标定位

        pm = new JPopupMenu();
        JMenuItem mi1 = new JMenuItem("打开");
        JMenuItem mi2 = new JMenuItem("保存");
        JMenuItem mi3 = new JMenuItem("退出");
        pm.add(mi1);
        pm.add(mi2);
        pm.add(mi3);
        this.addMouseListener(this);

        this.setLocationRelativeTo(null); // 使窗口居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 响应窗口关闭按钮
        this.setVisible(true); // 将窗口显示出来
    }
    public static void main(String[] args)
    {
        new MyFrame();
    }
    // 接口里都是抽象方法，如果派生类要想实例化出对象，必须全覆盖
    public void mouseClicked(MouseEvent e)
    {}
    public void mouseEntered(MouseEvent e)
    {}
    public void mouseExited(MouseEvent e)
    {}
    public void mousePressed(MouseEvent e)
    {}
    public void mouseReleased(MouseEvent e)
    {
        if(e.isPopupTrigger())
        {
            pm.show(this,e.getX(),e.getY());
        }
    }
}